
package com.zjxd.business.web.api.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.zjxd.business.web.code.ApiResultCode;
import com.zjxd.business.web.common.ApiResult;
import com.zjxd.business.web.common.BaseController;
import com.zjxd.business.web.model.pojo.TBUser;

/**
 * 用户管理
 * 
 * @author dj 2018/07/02
 */
@RestController
@RequestMapping("api/user")
public class ApiUserController extends BaseController {

	/**
	 * 获得用户分页列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/listPage")
	public ApiResult listPage(ApiUserModel model) {
		
		return ApiResult.success(apiUserService.getAll(model));
	}

	/**
	 * 删除用户信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public ApiResult delete(String ids) {

		return apiUserService.deleteByIds(ids);
	}

	/**
	 * 新增用户信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public ApiResult insert(HttpServletRequest request, TBUser tBUser) {

		// 判断用户对象是否为空
		return apiUserService.insert(tBUser);
	}

	/**
	 * 修改用户信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ApiResult update(HttpServletRequest request, TBUser tBUser) {

		// 判断用户对象是否为空
		return apiUserService.update(tBUser);
	}

	/**
	 * 导出用户信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/export", method = RequestMethod.POST)
	public ApiResult export(HttpServletRequest request, String ids, HttpServletResponse response) {

		if (ids == null || ids == "") { return ApiResult.fail(ApiResultCode.parameter_error_code); }
		int falge = apiUserService.export(ids, response);
		if (falge > 0) {
			return ApiResult.success(falge);
		} else {
			return ApiResult.fail(ApiResultCode.fail_code);
		}
	}

	/**
	 * 用户充值金额信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/updateBalance", method = RequestMethod.POST)
	public ApiResult update(HttpServletRequest request) {

		TBUser tBUser = new TBUser();

		// 获得用户id
		String userid = request.getParameter("userid");
		if (userid != null && !userid.trim().equals("")) {
			tBUser.setUserid(Long.parseLong(userid));
		} else {
			return ApiResult.fail(ApiResultCode.parameter_error_code);
		}

		// 获得充值金额
		String money = request.getParameter("money");
		if (money != null && !money.trim().equals("")) {
			tBUser.setMoney(Long.parseLong(money));
		} else {
			return ApiResult.fail(ApiResultCode.parameter_error_code);
		}

		return apiUserService.updateBalance(tBUser);
	}

}
